<?php

class DB
{
	private	$link;
	private	$result;
	
	public function	__construct()
	{
		$this->result = array();
		$this->connect();
		$this->select_db();
	}
	
	private function select_db()
	{
		if (mysql_select_db(DB_NAME, $this->link) == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_select_db() Error : '.mysql_error($this->link);
			exit;
		}
	}
	
	private function connect()
	{
		$this->link = mysql_connect(DB_HOST, DB_USER, DB_PASS, true);
		if ($this->link == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_connect() Error : '.mysql_error();
			exit;
		}
	}
	
	public function insert_id()
	{
		if (($id = mysql_insert_id($this->link)) == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_insert_id() Error : '.mysql_error();
			exit;
		}
		return $id;
	}
	
	public function esc($string)
	{
		if (($string = mysql_real_escape_string($string, $this->link)) == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_real_escape_string() Error : '.mysql_error();
			exit;
		}
		return $string;
	}
	
	public function query($query)
	{
		$this->result = mysql_query($query, $this->link);
		if ($this->result == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_query() Error : '.mysql_error($this->link);
			exit;
		}
	}
	
	public function fetch_array($type = MYSQL_ASSOC)
	{
		return mysql_fetch_array($this->result, $type);
	}
	
	public function fetch_row()
	{
		return mysql_fetch_row($this->result);
	}
	
	
	public function num_rows()
	{
		if (($res = mysql_num_rows($this->result)) == false)
		{
			/*if (defined('DEBUG') == true)
			echo 'mysql_num_rows() Error : '.mysql_error($this->link);
			exit;*/
		}
		return $res;
	}
	
	public function free()
	{
		if (mysql_free_result($this->result) == false)
		{
			if (defined('DEBUG') == true)
			echo 'mysql_free_result() Error : '.mysql_error($this->link);
			exit;
		}
	}
	
	private function close()
	{
		mysql_close($this->link);
	}
	
	public function __destruct()
	{
		$this->close();
		unset($this->link);
		unset($this->result);
	}
}

?>